package cn.ecut.session;

import cn.ecut.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;


@WebServlet("/success")
public class SuccessServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();

        HttpSession session = request.getSession();

        Object user = session.getAttribute("user");
        // 其实就是获取 jSessionID 。
        String id = session.getId();
        System.out.println( id  );
        // 这里 就表示了 session 的过期时间 （1800s ，也就是 30分钟）
        int maxInactiveInterval = session.getMaxInactiveInterval();
        System.out.println( maxInactiveInterval );
        // 这里隐含了一个条件， 即 user != null
        if( user instanceof User ){
            User u = (User)user ;
            writer.write("<h1>欢迎你 ： "+u.getName() +"</h1>");
        }else{
            writer.write("<h1>还没有登录</h1>");
        }
    }
}
